package com.erp.servlet;

import com.erp.bean.Commodity;
import com.erp.bean.Emp;
import com.erp.bean.Order;
import com.erp.service.CommodityService;
import com.erp.service.EmpService;
import com.erp.service.OrderService;
import com.erp.service.RoleService;
import com.erp.service.impl.CommodityServiceImpl;
import com.erp.service.impl.EmpServiceImpl;
import com.erp.service.impl.OrderServiceImpl;
import com.erp.util.AutoGenerationOrderID;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.Timestamp;
import java.util.Date;

/**
 * @author lyb
 * 创建补货发起采购订单控制器
 */
@WebServlet("/addinventory.action")
public class AddinventoryServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取页面信息
        String sid=req.getParameter("sid");
        int si=Integer.valueOf(sid);
        String ename=req.getParameter("ename");
        //创建员工业务逻辑对象
        String number=req.getParameter("number");
        int num=Integer.valueOf(number);
        EmpService empService=new EmpServiceImpl();
        Emp emp=empService.findByName(ename);
        String comname=req.getParameter("comname");
        //创建商品业务逻辑对象
        CommodityService commodityService=new CommodityServiceImpl();
        Commodity commodity=commodityService.findByGname(comname);
        String oid=AutoGenerationOrderID.createOrderId();
        Timestamp time=new Timestamp(new Date().getTime());
        //创建订单业务逻辑对象
        Order order=new Order(oid,si,emp.getEid(),time,commodity.getGid(),num);
        OrderService orderService=new OrderServiceImpl();
        orderService.add(order);
        //重定向到订单列表页面
        resp.sendRedirect("inventoryall.action");
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
